package cn.qing.controller;

import cn.qing.pojo.Member;
import cn.qing.pojo.Orders;
import cn.qing.pojo.Product;
import cn.qing.service.OrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 查找全部
     */
    @RequestMapping("findAll")
    public ModelAndView findAll(
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @RequestParam(name = "size",defaultValue = "5") Integer size){

        ModelAndView mv = new ModelAndView();

        List<Orders> orderlist = orderService.findAll(page,size);
        PageInfo<Orders> pageInfo = new PageInfo<>(orderlist);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("order-list");
        return mv;
    }

    /**
     * 查找订单详情
     * @return
     */
    @RequestMapping("findById")
    public ModelAndView findById(@RequestParam Integer id){
        ModelAndView mv = new ModelAndView();
        Orders orders = orderService.findById(id);
        mv.addObject("order",orders);
        mv.setViewName("order-show");
        return mv;
    }

    /**
     * 添加页面
     * @return
     */
    @RequestMapping("add")
    public ModelAndView add(){
        ModelAndView mv = new ModelAndView();
        List<Product> productList = orderService.findAllProduct();
        List<Member> memberList = orderService.findAllMember();

        mv.addObject("productList",productList);
        mv.addObject("memberList",memberList);
        mv.setViewName("order-add");
        return mv;
    }

    @RequestMapping("save")
    public String save(Orders orders){
        orderService.save(orders);
        return "redirect:findAll.do";
    }
}
